package com.huawei.crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huawei.crm.domain.Employee;
import com.huawei.crm.mapper.EmployeeMapper;
import com.huawei.crm.mapper.PermissionMapper;
import com.huawei.crm.query.EmployeeQueryObject;
import com.huawei.crm.service.IEmployeeService;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper mapper;

    @Autowired
    private PermissionMapper permissionMapper;

    public void saveOrUpdate(Employee e, Long[] roleIds) {
        if (e.getId() == null) {
            // 使用MD5密码做加密
            String password = new Md5Hash(e.getPassword(), e.getName()).toString();
            e.setPassword(password);
            mapper.insert(e);
        } else {
            mapper.updateByPrimaryKey(e);
            //删除旧关系
            mapper.deleteEmpAndRoleRelation(e.getId(), null);
        }

        //保存用户和角色的关系
        if (roleIds != null) {
            for (Long roleId : roleIds) {
                mapper.insertEmpAndRoleRelation(e.getId(), roleId);
            }
        }
    }

    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
        //删除旧关系
        mapper.deleteEmpAndRoleRelation(id, null);
    }

    public void batchDelete(Long[] ids) {
        mapper.deleteByIds(ids);
    }

    @Transactional(readOnly = true)
    public Employee get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Transactional(readOnly = true)
    public List<Employee> listAll() {
        return mapper.selectAll();
    }

    @Transactional(readOnly = true)
    public PageInfo<Employee> query(EmployeeQueryObject qo) {
        // 告诉mybatis需要去哪里拼接sql
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Employee> list = mapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    public Workbook exportXls() {
        Workbook book = new HSSFWorkbook();
        // 写数据
        List<Employee> employees = mapper.selectAll();
        // 创建表格
        Sheet sheet = book.createSheet("员工信息");
        // 创建行
        // 创建表头
        Row header = sheet.createRow(0);
        // 创建单元格
        // 在单元格中写数据
        header.createCell(0).setCellValue("姓名");
        header.createCell(1).setCellValue("年龄");
        header.createCell(2).setCellValue("邮箱");
        for (int i = 0; i < employees.size(); i++) {
            Row row = sheet.createRow(i + 1);
            // 创建单元格
            // 在单元格中写数据
            row.createCell(0).setCellValue(employees.get(i).getName());
            row.createCell(1).setCellValue(employees.get(i).getAge());
            row.createCell(2).setCellValue(employees.get(i).getEmail());
        }
        return book;
    }

    public void improtXls(InputStream inputStream) throws IOException {
        // 根据文件输入流创建工作簿
        HSSFWorkbook book = new HSSFWorkbook(inputStream);
        // 获取表格
        HSSFSheet sheet = book.getSheet("员工信息");

        for (int i = 0; i < sheet.getLastRowNum(); i++) {
            // 获取行
            HSSFRow row = sheet.getRow(i + 1);
            // 获取单元格
            // 获取单元格中的数据
            String name = row.getCell(0).getStringCellValue();
            int age = Double.valueOf(row.getCell(1).getNumericCellValue()).intValue();
            String email = row.getCell(2).getStringCellValue();

            // 将获取到的数据保存到数据库中
            Employee e = new Employee();
            e.setName(name);
            e.setAge(age);
            e.setEmail(email);
            e.setPassword(new Md5Hash("1", name).toString());
            mapper.insert(e);
        }
    }

    public List<Employee> listEmpsByRoleSns(String... roleSns) {
        return mapper.selectByRoleSns(roleSns);
    }

}
